﻿// JScript 文件
var qid = 1;
$(document).ready(function(){

    $("#drpQN").change(function(){
        RefQuestion();
    }); 
    
    $("#drpQuestion").change(function(){
        qid = $("#drpQuestion").val(); 
        InitChart();
    });
    
    RefQuestion();
});

function RefQuestion(){
    var qnid = $("#drpQN").val();
    if(!qnid) qnid="";
    GetQuestion(qnid);
}

function GetQuestion(qnid){
    var html="";
    $.ajax({
        type:"GET",
        url:"Comsrv/StatSrv.aspx",
        dataType:"json",
        data:"oper=GetQuestion&qnid="+qnid,
        success:function(data){
            
            if(data.length>0){
                for(var i=0;i<data.length;i++){
                    html+="<option  value='"+data[i].Id.toString()+"'>"+data[i].QUESTION+"</option>";
                } 
            }
            $("#drpQuestion").html(html);
            InitChart();
        }        
    });
}

//格式化图表
function InitChart()
{
    
     var total = 0;
     if (qid)
     {
         $.ajax({
            type:"GET",
            url:"Comsrv/StatSrv.aspx",
            dataType:"json",
            data:"oper=GetStat&qid="+qid,
            success:function(data){
              var ArrStat = data;
              
              if (ArrStat.length>0)
              {
                var line = new Array();
                line[0] = new Array(); 
                for(var i = 0; i<ArrStat.length;i++)
                {
                    line[i] = new Array();
                    line[i][0] = ArrStat[i].label;
                    line[i][1] = parseInt(ArrStat[i].num);
                    total += parseInt(ArrStat[i].num);
                }
              }
              formatTable(ArrStat,total);
              if($("#QuestionType").val()!="singleblank")
              {
                var movie = document.getElementsByName("suer");
                alert(movie);
                movie.ChangeQuestion(qid);
              }
              else
              {
                formatBlank(qid);
              }
              
              
            } 
              
         });
     }
}

function formatTable(ArrStat,total)
{
    $("#resTable").empty();
    var res = "<table><thead><tr><td>选项</td><td>选择个数</td><td>百分比</td></tr></thead>";
    for(var i = 0; i<ArrStat.length;i++)
    {
        res +="<tr>"
        res +="<td align='right'>"+ArrStat[i].label+"</td>";
        res +="<td>"+ArrStat[i].num+"</td>"
        res +="<td>"+Math.round((ArrStat[i].num/total)*1000)/10+"%</td>"
        res +="</tr>"
    }
    res += "<tr><td align='right'>总计：</td><td>"+total+"</td><td></td></tr>"
    res += "</table>" 
    $("#resTable").html(res);
    
}


 
 function formatBlank(qid)
 {
    
    var res = "<ul>"
    if (qid)
     {
         $.ajax({
            type:"GET",
            url:"Comsrv/StatSrv.aspx",
            dataType:"json",
            data:"oper=GetBlank&qid="+qid,
            success:function(data){
                for( var i = 0;i<data.length;i++)
                {
                    res+="<li title='"+data[i].ANSWERTEXT+"'>"+data[i].ANSWERTEXT.substring(0,20)+"</li>";  
                }
                res += "</ul>"
                $("#chart").html(res);   
            } 
                 
         });
     }
 }